package com.forum.service.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author lbwxxc
 * @date 2025/4/10 20:13
 * @description: redis
 */
@Component
@Slf4j
public class RedisUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // 设置 key 的过期时间
    public void expire(String key, Long time, TimeUnit timeUnit) {
        stringRedisTemplate.expire(key, time, timeUnit);
    }

    public void expire(String key, Long time) {
        stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    //删除缓存
    public void delete(String key) {
        stringRedisTemplate.delete(key);
    }

    // ZSet 中的 filed 字段增加 cnt
    public Double ZSetIncr(String key, String field, Integer cnt) {
        return stringRedisTemplate.opsForZSet().incrementScore(key, field, cnt);
    }

    // ============== String 类型操作 ==============
    // 设置键值对
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    // 设置键值对并设置过期时间
    public void set(String key, String value, long timeout, TimeUnit unit) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    // 获取键对应的值
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    // ============== Hash 类型操作 ==============
    // 向哈希表中添加一个字段和值
    public int hashIncr(String key, String field, int value) {
        Long increment = stringRedisTemplate.opsForHash().increment(key, field, value);
        return increment.intValue();
    }
    public int hashIncr(String key, String field, int value, long timeout, TimeUnit unit) {
        Long increment = stringRedisTemplate.opsForHash().increment(key, field, value);
        stringRedisTemplate.expire(key, timeout, unit);
        return increment.intValue();
    }

    // 获取哈希表中指定字段的值
    public String hashGet(String key, String field) {
        return (String) stringRedisTemplate.opsForHash().get(key, field);
    }

    // 获取哈希表中所有字段和值
    public Map<Object, Object> HashGetAll(String key) {
        return stringRedisTemplate.opsForHash().entries(key);
    }

    // 删除哈希表中指定字段
    public Long HashDelete(String key, Object... fields) {
        return stringRedisTemplate.opsForHash().delete(key, fields);
    }

    // ============== List 类型操作 ==============
    // 从列表左侧插入元素
    public Long lPush(String key, String value) {
        return stringRedisTemplate.opsForList().leftPush(key, value);
    }

    // 从列表右侧弹出元素
    public String rPop(String key) {
        return stringRedisTemplate.opsForList().rightPop(key);
    }

    // 获取列表指定范围内的元素
    public List<String> lRange(String key, long start, long end) {
        return stringRedisTemplate.opsForList().range(key, start, end);
    }

    // ============== Set 类型操作 ==============
    // 向集合中添加元素
    public Long setAdd(String key, String... values) {
        return stringRedisTemplate.opsForSet().add(key, values);
    }

    // 获取集合中的所有元素
    public Set<String> setMembers(String key) {
        return stringRedisTemplate.opsForSet().members(key);
    }

    // 从集合中移除元素
    public Long setDelete(String key, Object... values) {
        return stringRedisTemplate.opsForSet().remove(key, values);
    }


}
